package com.qf.utils.http.httpclient;


import com.qf.utils.SmallTool;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpClientUtil {

    /**
     * 转化各个httpclient返回的html
     */
    public static String parsingHtml(CloseableHttpResponse responce) throws Exception {
        String html = null;
        HttpEntity entity = responce.getEntity();
        if (entity != null) {
            html = EntityUtils.toString(entity, "UTF-8");
            html = html.replace("&nbsp;", " ");
        }
        return html;
    }

    /**
     * 根据各个资源的路径生成对应的pdf
     */
    public static void createFile(String filePath, String fileName, byte[] content)
            throws IOException {
        File folder = new File(filePath);
        if (!folder.exists() && !folder.isDirectory()) {
            SmallTool.printMess("文件夹路径不存在，创建路径:" + filePath);
            folder.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(filePath + "/" + fileName);
        fos.write(content);
        fos.close();
    }


    /**
     * 配置httprequest的方法
     */
    public static RequestConfig buildRequestConfigure(Boolean redirectFlag, int timeout) {
        if (timeout == 0) {
            timeout = HttpClientConfig.TIME_OUT;
        }
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(timeout).setConnectTimeout(timeout).setRedirectsEnabled(redirectFlag)
                .build();
        return requestConfig;
    }

    /**
     * 通过代理信息生成相应的检索配置信息
     */
    public static HttpClientConfig getSearConfig(String url, String refer, String host, boolean redireFlag) {
        HttpClientConfig config = new HttpClientConfig();
        config.setUrl(url);
        config.setHost(host);
        config.setRefer(refer);
        Map<String, String> headerMap = new HashMap<String, String>();
        if (!StringUtils.isEmpty(host)) {
            headerMap.put(HttpHeaders.HOST, host);
        }
        if (!StringUtils.isEmpty(refer)) {
            headerMap.put(HttpHeaders.REFERER, refer);
        }

        config.setHeaderMap(headerMap);
        RequestConfig requestConfig = HttpClientUtil.buildRequestConfigure(redireFlag, 0);
        config.setRequestConfig(requestConfig);

        return config;
    }

    public static HttpClientConfig getSearConfig(String url) {
        HttpClientConfig config = new HttpClientConfig();
        config.setUrl(url);
        config.setHost("");
        config.setRefer("");
        Map<String, String> headerMap = new HashMap<String, String>();
        if (!StringUtils.isEmpty("")) {
            headerMap.put(HttpHeaders.HOST, "");
        }
        if (!StringUtils.isEmpty("")) {
            headerMap.put(HttpHeaders.REFERER, "");
        }

        config.setHeaderMap(headerMap);
        RequestConfig requestConfig = HttpClientUtil.buildRequestConfigure(true, 0);
        config.setRequestConfig(requestConfig);

        return config;
    }

    /**
     * 通过代理信息生成相应的检索配置信息
     */
    public static HttpClientConfig getSearConfig(String url, String refer, String host, boolean redireFlag, int timeOut) {
        HttpClientConfig config = new HttpClientConfig();
        config.setUrl(url);
        config.setHost(host);
        config.setRefer(refer);
        Map<String, String> headerMap = new HashMap<String, String>();
        if (!StringUtils.isEmpty(host)) {
            headerMap.put(HttpHeaders.HOST, host);
        }
        if (!StringUtils.isEmpty(refer)) {
            headerMap.put(HttpHeaders.REFERER, refer);
        }

        config.setHeaderMap(headerMap);
        RequestConfig requestConfig = HttpClientUtil.buildRequestConfigure(redireFlag, timeOut);
        config.setRequestConfig(requestConfig);

        return config;
    }


    /**
     * 打印cookie查看问题的方法
     */
    public static void printCookie(BasicCookieStore bcs) {
        List<Cookie> list = bcs.getCookies();
        for (Cookie c : list) {
            SmallTool.printMess("cookie信息:" + c.toString());
        }
    }

}
